package com.punai.customer.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.utils.PageUtils;
import com.punai.common.utils.SecurityUtils;
import com.punai.customer.domain.CusMechanicalStaItem;
import com.punai.customer.domain.vo.MechanicallVo;
import com.punai.customer.mapper.CusMechanicalStaChapterMapper;
import com.punai.customer.mapper.CusMechanicalStaItemMapper;
import com.punai.customer.service.ICusMechanicalStaItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 机械费用项Service业务层处理
 *
 * @author zxw_
 * @date 2023-02-02
 */
@Service
public class CusMechanicalStaItemServiceImpl extends ServiceImpl<CusMechanicalStaItemMapper,CusMechanicalStaItem> implements ICusMechanicalStaItemService
{
    @Autowired
    private CusMechanicalStaItemMapper cusMechanicalStaItemMapper;
    @Autowired
    private CusMechanicalStaChapterMapper cusMechanicalStaChapterMapper;

    /**
     * 查询机械费用项
     *
     * @param id 机械费用项主键
     * @return 机械费用项
     */
    @Override
    public CusMechanicalStaItem selectCusMechanicalStaItemById(String id)
    {
        return cusMechanicalStaItemMapper.selectCusMechanicalStaItemById(id);
    }

    /**
     * 查询机械费用项列表
     *
     * @param cusMechanicalStaItem 机械费用项
     * @return 机械费用项
     */
    @Override
    public List<CusMechanicalStaItem> selectCusMechanicalStaItemList(CusMechanicalStaItem cusMechanicalStaItem)
    {
        return cusMechanicalStaItemMapper.selectCusMechanicalStaItemList(cusMechanicalStaItem);
    }

    /**
     * 新增机械费用项
     *
     * @param cusMechanicalStaItem 机械费用项
     * @return 结果
     */
    @Override
    public int insertCusMechanicalStaItem(CusMechanicalStaItem cusMechanicalStaItem)
    {
            return cusMechanicalStaItemMapper.insert(cusMechanicalStaItem);
    }

    /**
     * 修改机械费用项
     *
     * @param cusMechanicalStaItem 机械费用项
     * @return 结果
     */
    @Override
    public int updateCusMechanicalStaItem(CusMechanicalStaItem cusMechanicalStaItem)
    {
        return cusMechanicalStaItemMapper.updateById(cusMechanicalStaItem);
    }

    /**
     * 批量删除机械费用项
     *
     * @param ids 需要删除的机械费用项主键
     * @return 结果
     */
    @Override
    public int deleteCusMechanicalStaItemByIds(String[] ids)
    {
        return cusMechanicalStaItemMapper.deleteCusMechanicalStaItemByIds(ids);
    }

    /**
     * 删除机械费用项信息
     *
     * @param id 机械费用项主键
     * @return 结果
     */
    @Override
    public int deleteCusMechanicalStaItemById(String id)
    {
        return cusMechanicalStaItemMapper.deleteCusMechanicalStaItemById(id);
    }

    @Override
    public TableDataInfo selectListData(MechanicallVo vo) {
        String itemName = vo.getItemName();
        String linkFlag = vo.getLinkFlag();
        String chapterId = vo.getChapterId();
        Long companyId = SecurityUtils.getCompanyId();

        List<CusMechanicalStaItem> cusMechanicalStaItems;
        Integer itemCount = -1;
        PageUtils.startPage();
        cusMechanicalStaItems = cusMechanicalStaItemMapper.selectLinkDataList(linkFlag,chapterId,companyId,itemName);
        if (StrUtil.isNotEmpty(chapterId)) {
            List<String> chapterIds = cusMechanicalStaChapterMapper.selectChildIdById(chapterId, companyId);
            chapterIds.add(0,chapterId);
            itemCount = cusMechanicalStaItemMapper.selectLinkDataListCount(linkFlag, chapterIds, companyId);

        }
        Long total = new PageInfo(cusMechanicalStaItems).getTotal();
        TableDataInfo tableDataInfo = new TableDataInfo(cusMechanicalStaItems, total.intValue());
        tableDataInfo.setMsg(itemCount.toString());
        return tableDataInfo;
    }
}
